(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.docTemplate.viewController', viewController);
	viewController.$inject = [ '$scope', 'Constants', 'layoutConfig', 'validatorConfig', 'gridService', 'authService', 'docTemplateService' ];
	function viewController($scope, Constants, layoutConfig, validatorConfig, gridService, authService, docTemplateService ) {
		var ctrl = this, docTemplateInfo = $scope.$stateParams.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.docTemplate = {};
		ctrl.docTemplate.docTempPreParms = ctrl.docTemplate.docTempPreParms || [];
		ctrl.docTemplate.docTempPostParms = ctrl.docTemplate.docTempPostParms || [];
		
		ctrl.init = function() {	
			//init dTempParms
			ctrl.innerPreGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.docTempPreParms.tableConfigs.table, rowSelectionChanged);
			ctrl.innerPreGridOptions.data = 'vm.docTemplate.docTempPreParms';
			ctrl.innerPostGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.docTempPostParms.tableConfigs.table, rowSelectionChanged);
			ctrl.innerPostGridOptions.data = 'vm.docTemplate.docTempPostParms';
			ctrl.innerPreGridOptions.enableRowHeaderSelection = false;
			//postParms
			ctrl.innerPostGridOptions.enableRowHeaderSelection = false;

			function rowSelectionChanged(eventRow, selectedRows) {
				ctrl.selectedDTParmList = selectedRows;
			}
			// init docTemplate Parameter
			docTemplateService.initDataOptions($scope, ctrl);
			
			authService.view(docTemplateInfo).then(function(response) {
				ctrl.docTemplate = response.data || docTemplateInfo;
				// "{name: "S", type: "java.lang.String", desc: "S", value: "S"}"
				ctrl.docTemplate.preprocParms = ctrl.docTemplate.preprocParms;
				ctrl.docTemplate.postprocParms = ctrl.docTemplate.postprocParms;
				
				ctrl.docTemplate.docTempPreParms = [];
				ctrl.docTemplate.docTempPostParms = [];
				
				//set parms from string parms(preprocParms,postprocParms)
				if (ctrl.docTemplate.preprocParms != null ) {	
					if (ctrl.docTemplate.preprocParms.split("},{").length > 0) {
						//"{"name":"21","type":"java.lang.String","desc":"21","value":"21"},{"name":"22","type":"java.lang.String","desc":"22","value":"22"}"
						 var a = ctrl.docTemplate.preprocParms.replace(/},{/g, "},,{");
						//"{"name":"21","type":"java.lang.String","desc":"21","value":"21"},,{"name":"22","type":"java.lang.String","desc":"22","value":"22"}"
						var b = a.split(",,");
						angular.forEach(b, function (value,key){
							ctrl.docTemplate.docTempPreParms.push(angular.fromJson(angular.fromJson(angular.toJson(value))));
						});
					} else {
						ctrl.docTemplate.docTempPreParms.push(angular.fromJson(angular.fromJson(angular.toJson(ctrl.docTemplate.preprocParms))));
					}
				}
				if (ctrl.docTemplate.postprocParms != null) {		
					if (ctrl.docTemplate.postprocParms.split("},{").length > 0) {
						//"{"name":"21","type":"java.lang.String","desc":"21","value":"21"}, {"name":"22","type":"java.lang.String","desc":"22","value":"22"}"
						 var a = ctrl.docTemplate.postprocParms.replace(/},{/g, "},,{");
						//"{"name":"21","type":"java.lang.String","desc":"21","value":"21"},, {"name":"22","type":"java.lang.String","desc":"22","value":"22"}"
						var b = a.split(",,");
						angular.forEach(b, function (value,key){
							ctrl.docTemplate.docTempPostParms.push(angular.fromJson(angular.fromJson(angular.toJson(value))));
						});
					} else {
						ctrl.docTemplate.docTempPostParms.push(angular.fromJson(angular.fromJson(angular.toJson(ctrl.docTemplate.postprocParms))));
					}
				}
				
			}, angular.noop);
		};
		
	}
})(angular);